package com.nd.mobile.study.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.nd.mobile.study.R;

public class SimpleTabsFragmentAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
	private final Activity	mContext;
	private final TabHost	mTabHost;
	private final ViewPager	mViewPager;
	private int				mTabConter;
	private Fragment[]		mFragments;
	private String[]		mFragmentNames;

	private static class DummyTabFactory implements TabHost.TabContentFactory {
		private final Context	mContext;

		public DummyTabFactory(Context context) {
			mContext = context;
		}

		@Override
		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}
	}

	public SimpleTabsFragmentAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
		super(activity.getSupportFragmentManager());
		mContext = activity;
		mTabHost = tabHost;
		mViewPager = pager;
		mTabHost.setOnTabChangedListener(this);
		mViewPager.setAdapter(this);
		mViewPager.setOnPageChangeListener(this);
	}

	public void setTabs(String[] labels, String[] fragmentNames) {
		mTabConter = labels.length;
		mFragments = new Fragment[mTabConter];
		mFragmentNames = fragmentNames;
		for (String label : labels) {
			TabSpec tabSpec = mTabHost.newTabSpec(label);
			tabSpec.setIndicator(createIndicator(label));
			tabSpec.setContent(new DummyTabFactory(mContext));
			mTabHost.addTab(tabSpec);
		}
		notifyDataSetChanged();
	}

	private View createIndicator(String label) {
		// 获取整个标签页的view
		View indicatorView = LayoutInflater.from(mContext).inflate(R.layout.pager_tab_item, null);
		// 设置文字
		TextView tvLabel = (TextView) indicatorView.findViewById(R.id.pager_tab_label);
		tvLabel.setText(label);
		return indicatorView;
	}

	@Override
	public int getCount() {
		return mTabConter;
	}

	// @Override
	// public Object instantiateItem(ViewGroup container, int position) {
	// View view = null;
	// view = getPageView(position);
	// container.addView(view, 0);
	// if (!mIsExisted[position]) {
	// initPageView(position);
	// mIsExisted[position] = true;
	// }
	// return view;
	// }

	@Override
	public Fragment getItem(int position) {
		Fragment f = null;
		if (null == mFragments[position]) {
			f = Fragment.instantiate(mContext, mFragmentNames[position]);
			mFragments[position] = f;
		}
		return f;
	}

	@Override
	public void onTabChanged(String tabId) {
		int position = mTabHost.getCurrentTab();
		mViewPager.setCurrentItem(position, true);
	}

	@Override
	public void destroyItem(View collection, int position, Object view) {
		((ViewPager) collection).removeView((View) view);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void onPageSelected(int position) {
		// Unfortunately when TabHost changes the current tab, it kindly
		// also takes care of putting focus on it when not in touch mode.
		// The jerk.
		// This hack tries to prevent this from pulling focus out of our
		// ViewPager.
		TabWidget widget = mTabHost.getTabWidget();
		int oldFocusability = widget.getDescendantFocusability();
		widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		mTabHost.setCurrentTab(position);
		widget.setDescendantFocusability(oldFocusability);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}
}